"""
Copyright (c) 2024 Beijing Jiaotong University
PhotLab is licensed under [Open Source License].
You can use this software according to the terms and conditions of the [Open Source License].
You may obtain a copy of [Open Source License] at: [https://open.source.license/]

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

See the [Open Source License] for more details.

Author: Yunzhou Tang
Created: 2024/02/14
Supported by: National Key Research and Development Program of China
"""

from ..optical import *
from ..utils import *
from . import *


def transmitter(
    num_symbols: int = 2**16,
    bits_per_symbol: int = 4,
    total_baud: float = 60e9,
    up_sampling_factor: int = 4,
    Reference_frequency: float = 193.1e12,
    roll_off: float = 0.01,
    DAC_Resolution_Bits: int = 7,
    DAC_OUTPUT_VPP: int = 1,
    Linewidth: float = 1e3,
    output_power: int = 20,
    Extinction_ratio: int = 65,
    VPI: int = 5,
    VDC: float = -2.5,
    osnr: int = 32
):

    # 首先产生发射端X/Y双偏振信号
    signal_bits = gen_bits(num_symbols, bits_per_symbol)

    # 生成双偏振符号序列 #
    signals = modulation(signal_bits, bits_per_symbol)

    # 均衡的训练序列
    prev_symbols = signals
    
    # 上采样 #
    signals = up_sample(signals, up_sampling_factor)

    # RRC #
    signals = pulse_shaper(signals, up_sampling_factor,
                           roll_off, total_baud)

    # DAC #
    signals = dac(signals, DAC_Resolution_Bits, DAC_OUTPUT_VPP)

    """ 光源 """
    laser_center_frequency = Reference_frequency
    sampling_rate = up_sampling_factor * total_baud  # 信号采样率
    N_symbol=num_symbols*up_sampling_factor
    Optical_tx_laser = laser_cw(N_symbol, laser_center_frequency,
                                Reference_frequency, sampling_rate, output_power, Linewidth)

    """ 调制器 """

    signals = iq_modulator(signals, Optical_tx_laser,
                           VDC, Extinction_ratio, VPI)

    """ 根据OSNR添加高斯白噪声 """
    signals = gaussian_noise(signals, osnr, sampling_rate)

    #将待传输信号与均衡的训练序列信号合并为一个变量 signals长度为4 前两个位置存放待传输信号 后两个位置存放均衡的训练序列信号
    signals.append(prev_symbols[0])
    signals.append(prev_symbols[1])

    return signals
